<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>统计对象的元素个数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.countable.html">« Countable</a></li>
      <li style="float: right;"><a href="class.outeriterator.html">OuterIterator »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.countable.html">Countable</a></li>
    <li>统计对象的元素个数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="countable.count" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Countable::count</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">Countable::count</span> &mdash; <span class="dc-title">统计对象的元素个数</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-countable.count-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Countable::count</strong></span>(): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   当在实现了 <span class="classname"><a href="class.countable.html" class="classname">Countable</a></span> 的对象上执行 <span class="function"><a href="function.count.html" class="function">count()</a></span>
   函数时，会执行此方法。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-countable.count-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-countable.count-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="type">int</span> 形式的自定义计数。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    返回值强制转换为 <span class="type">int</span>。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 examples" id="refsect1-countable.count-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3657">
    <p><strong>示例 #1 <span class="function"><strong>Countable::count()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myCounter&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">Countable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$count&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;++</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">count</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$counter&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myCounter</span><span style="color: #007700">;<br /><br />for(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;have&nbsp;been&nbsp;count()ed&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$counter</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;times\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
I have been count()ed 1 times
I have been count()ed 2 times
I have been count()ed 3 times
I have been count()ed 4 times
I have been count()ed 5 times
I have been count()ed 6 times
I have been count()ed 7 times
I have been count()ed 8 times
I have been count()ed 9 times
I have been count()ed 10 times
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>